package com.ucex.common.rocketmq.consumer;

import com.ucex.common.rocketmq.MQProperties;

/**
 * 消息接受处理类
 */
public abstract class AbstractMessageConsumerProcess {

    public void registerListener(MessageConsumer listener) {
        String topic = listener.getTopic();
        if (isEmpty(topic)) {
            return;
        }
        String tag = listener.getTag();

        //如果tag未指定，则监听topic下面所有消息
        if (isEmpty(tag)) {
            tag = "*";
        }

        //消息处理
        handlerMessage(listener);
    }

    private boolean isEmpty(String topic) {
        return null == topic || 0 == topic.trim().length() || "null".equals(topic);
    }

    /**
     * 初始化MQ中的消费对象
     * @param properties
     */
    public abstract void initConsumer(MQProperties properties);

    /**
     * 启动消费对象
     */
    public abstract void start();

    /**
     * 处理从mq中接受的数据
     * @param listener
     */
    public abstract void handlerMessage(MessageConsumer listener);
}
